#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <vector>
#include <cstdio>

#if 0
// https://www.luogu.com.cn/problem/P8879

const int MAXN = 100002;
const long long INF = 9223372036854775807LL;

int n, m, q;
int father[MAXN];
long long dist[MAXN];

void prepare() {
    for (int i = 0; i <= n; ++i) {
        father[i] = i;
        dist[i] = 0;
    }
}

int find(int i) {
    if (i != father[i]) {
        int tmp = father[i];
        father[i] = find(tmp);
        dist[i] += dist[tmp];
    }
    return father[i];
}

void myUnion(int l, int r, long long v) {
    int lf = find(l), rf = find(r);
    if (lf != rf) {
        father[lf] = rf;
        dist[lf] = v + dist[r] - dist[l];
    }
}

long long query(int l, int r) {
    if (find(l) != find(r)) {
        return INF;
    }
    return dist[l] - dist[r];
}

int main() {
    scanf("%d", &n);
    n += 1;
    scanf("%d", &m);
    scanf("%d", &q);
    prepare();
    int l, r;
    long long v;
    for (int i = 1; i <= m; ++i) {
        scanf("%d", &l);
        scanf("%d", &r);
        r += 1;
        scanf("%lld", &v);
        myUnion(l, r, v);
    }
    for (int i = 1; i <= q; ++i) {
        scanf("%d", &l);
        scanf("%d", &r);
        r += 1;
        v = query(l, r);
        if (v == INF) {
            printf("UNKNOWN\n");
        }
        else {
            printf("%lld\n", v);
        }
    }
    return 0;
}


// https://www.luogu.com.cn/problem/P2294

const int MAXN = 102;
int t, n, m;
bool ans;
int father[MAXN];
int dist[MAXN];

void prepare() {
    ans = true;
    for (int i = 1; i <= n; i++) {
        father[i] = i;
        dist[i] = 0;
    }
}

int find(int i) {
    if (i != father[i]) {
        int tmp = father[i];
        father[i] = find(tmp);
        dist[i] += dist[tmp];
    }
    return father[i];
}

void myUnion(int l, int r, int v) {
    int lf = find(l), rf = find(r);
    if (lf != rf) {
        father[lf] = rf;
        dist[lf] = v + dist[r] - dist[l];
    }
}

bool check(int l, int r, int v) {
    if (find(l) == find(r)) {
        if ((dist[l] - dist[r] != v)) {
            return false;
        }
    }
    return true;
}

int main() {
    scanf("%d", &t);
    for (int c = 1; c <= t; ++c) {
        scanf("%d", &n);
        n += 1;
        scanf("%d", &m);
        prepare();
        ans = true;
        for (int i = 1, l, r, v; i <= m; ++i) {
            scanf("%d", &l);
            scanf("%d", &r);
            r += 1;
            scanf("%lld", &v);
            if (!check(l, r, v)) {
                ans = false;
            }
            else {
                myUnion(l, r, v);
            }
        }
        printf("%s\n", ans ? "true" : "false");
    }
    return 0;
}

// https://www.luogu.com.cn/problem/P1196

#include <cstdio>
#include <cstdlib>

const int MAXN = 30001;

int n = 30000;
int father[MAXN];
int dist[MAXN];
int size[MAXN];
int stack[MAXN];

void prepare() {
    for (int i = 1; i <= n; ++i) {
        father[i] = i;
        dist[i] = 0;
        size[i] = 1;
    }
}

int find(int i) {
    int si = 0;
    while (i != father[i]) {
        stack[++si] = i;
        i = father[i];
    }
    stack[si + 1] = i;
    for (int j = si; j >= 1; --j) {
        father[stack[j]] = i;
        dist[stack[j]] += dist[stack[j + 1]];
    }
    return i;
}

void myUnion(int l, int r) {
    int lf = find(l), rf = find(r);
    if (lf != rf) {
        father[lf] = rf;
        dist[lf] += size[rf];
        size[rf] += size[lf];
    }
}

int query(int l, int r) {
    if (find(l) != find(r)) {
        return -1;
    }
    return abs(dist[l] - dist[r]) - 1;
}

int main() {
    prepare();
    int t;
    scanf("%d", &t);
    char op[2];
    for (int i = 1; i <= t; ++i) {
        scanf("%s", op);
        int l, r;
        scanf("%d %d", &l, &r);
        if (op[0] == 'M') {
            myUnion(l, r);
        }
        else {
            printf("%d\n", query(l, r));
        }
    }
    return 0;
}

// https://leetcode.cn/problems/evaluate-division/description/
#include <vector>
#include <string>
#include <unordered_map>

using namespace std;

class Solution {
public:
    vector<double> calcEquation(vector<vector<string>>& equations, vector<double>& values, vector<vector<string>>& queries) {
        prepare(equations);
        for (int i = 0; i < values.size(); ++i) {
            union_(equations[i][0], equations[i][1], values[i]);
        }
        vector<double> ans(queries.size());
        for (int i = 0; i < queries.size(); ++i) {
            ans[i] = query(queries[i][0], queries[i][1]);
        }
        return ans;
    }

private:
    unordered_map<string, string> father;
    unordered_map<string, double> dist;

    void prepare(vector<vector<string>>& equations) {
        father.clear();
        dist.clear();
        for (auto& list : equations) {
            for (auto& key : list) {
                father[key] = key;
                dist[key] = 1.0;
            }
        }
    }

    string find(string x) {
        if (father.find(x) == father.end()) {
            return "";
        }
        string tmp, fa = x;
        if (x != father[x]) {
            tmp = father[x];
            fa = find(tmp);
            dist[x] *= dist[tmp];
            father[x] = fa;
        }
        return fa;
    }

    void union_(string l, string r, double v) {
        string lf = find(l), rf = find(r);
        if (lf != rf) {
            father[lf] = rf;
            dist[lf] = dist[r] / dist[l] * v;
        }
    }

    double query(string l, string r) {
        string lf = find(l), rf = find(r);
        if (lf.empty() || rf.empty() || lf != rf) {
            return -1.0;
        }
        return dist[l] / dist[r];
    }
};


// https://www.luogu.com.cn/problem/P2024

#include <cstdio>

#define MAXN 50001

int n, k, ans;
int father[MAXN];
int dist[MAXN];

void prepare() {
    ans = 0;
    for (int i = 1; i <= n; ++i) {
        father[i] = i;
        dist[i] = 0;
    }
}

int find(int i) {
    if (i != father[i]) {
        int tmp = father[i];
        father[i] = find(tmp);
        dist[i] = (dist[i] + dist[tmp]) % 3;
    }
    return father[i];
}

void union_(int op, int l, int r) {
    int lf = find(l), rf = find(r), v = op == 1 ? 0 : 1;
    if (lf != rf) {
        father[lf] = rf;
        dist[lf] = (dist[r] - dist[l] + v + 3) % 3;
    }
}

bool check(int op, int l, int r) {
    if (l > n || r > n || (op == 2 && l == r)) {
        return false;
    }
    if (find(l) == find(r)) {
        if (op == 1) {
            if (dist[l] != dist[r]) {
                return false;
            }
        }
        else {
            if ((dist[l] - dist[r] + 3) % 3 != 1) {
                return false;
            }
        }
    }
    return true;
}

int main() {
    // 使用cstdio来代替iostream，减少对std::的依赖
    FILE* fp = stdin;
    fscanf(fp, "%d%d", &n, &k);
    prepare();
    int op, l, r;
    for (int i = 1; i <= k; ++i) {
        fscanf(fp, "%d%d%d", &op, &l, &r);
        if (!check(op, l, r)) {
            ans++;
        }
        else {
            union_(op, l, r);
        }
    }
    printf("%d\n", ans);
    return 0;
}

// https://www.luogu.com.cn/problem/P1525

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct crime {
    int a, b, c;
}x[100005];

int n, m, f[40005];
int ans;
int find(int k) {
    if (f[k] == k) {
        return k;
    }
    return f[k] = find(f[k]);
}
bool cmp(crime i, crime j) {
    return i.c > j.c;
}
int main() {
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= n; i++) {
        f[i] = i;
        f[i + n] = i + n;
    }
    for (int i = 1; i <= m; i++) {
        scanf("%d %d %d", &x[i].a, &x[i].b, &x[i].c);
    }
    sort(x + 1, x + m + 1, cmp);
    ans = 0;
    for (int i = 1; i <= m; i++) {
        int fa = find(x[i].a);
        int ffa = find(x[i].a + n);
        int fb = find(x[i].b);
        int ffb = find(x[i].b + n);
        if (fa == fb || ffa == ffb) {
            ans = x[i].c;
            break;
        }
        else {
            f[fa] = ffb;
            f[fb] = ffa;
        }
    }
    printf("%d", ans);
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
#include <array>

#define MAXN 20002
#define MAXM 100001

int n, m;
int father[MAXN];
int enemy[MAXN];
std::vector<std::array<int, 3>> arr; // 定义时不指定大小

void prepare() {
    for (int i = 1; i <= n; ++i) {
        father[i] = i;
        enemy[i] = 0;
    }
}

int find(int i) {
    return father[i] == i ? i : (father[i] = find(father[i]));
}

void union_(int l, int r) {
    father[find(l)] = find(r);
}

bool same(int l, int r) {
    return find(l) == find(r);
}

bool cmp(const std::array<int, 3>& a, const std::array<int, 3>& b) {
    return a[2] > b[2];
}

int compute() {
    std::sort(arr.begin() + 1, arr.end(), cmp); // 注意数组索引从1开始
    int ans = 0;
    for (int i = 1; i <= m; ++i) {
        int l = arr[i][0], r = arr[i][1], v = arr[i][2];
        if (same(l, r)) {
            ans = v;
            break;
        }
        else {
            if (enemy[l] == 0) {
                enemy[l] = r;
            }
            else {
                union_(enemy[l], r);
            }
            if (enemy[r] == 0) {
                enemy[r] = l;
            }
            else {
                union_(l, enemy[r]);
            }
        }
    }
    return ans;
}

int main() {
    std::cin >> n >> m;
    arr.resize(m + 1); // 根据m的实际值调整arr的大小
    prepare();
    for (int i = 1; i <= m; ++i) {
        std::cin >> arr[i][0] >> arr[i][1] >> arr[i][2];
    }
    std::cout << compute() << std::endl;
    return 0;
}

#endif
